<!-- find页面 阅读 -->
<template>
  <div>
    <div class="readBox" ref="readbox" @scroll="addData">
      <div
        v-for="item in data"
        :key="item.id"
        @click="
          goviewArticles(item.id, item.category, [{ title: '阅读' }], '阅读')
        "
      >
        <van-divider v-if="item.top" @click.stop>{{ item.top }}月</van-divider>
        <div class="readItem" v-if="item.title">
          <img :src="item.cover" alt="" />
          <div class="readInfo">
            <div>{{ item.title }}</div>
            <div>{{ item.subtitle }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRead } from "@/api/find.js";
export default {
  name: "read",
  props: ["newYear", "newMonth"],
  data() {
    return {
      data: [],
      now: null,
      year: 0,
      month: 0,
      day: 0,
      topmounth: null,
      oldYear: 0,
      oldMonth: 0,
      timer: null,
    };
  },
  created() {
    this.getReadData();
  },
  mounted() {
    // 监听滚动事件
    console.log();
  },
  methods: {
    async getReadData() {
      this.now = new Date(); //获取带有格式的时间
      this.year = this.newYear;
      this.month = this.newMonth; //月份从0开始，需要加1
      this.day = this.now.getDate();
      console.log("当前年月日", this.year, this.month, this.day);
      const { data } = await getRead(
        1,
        this.year + "-" + (this.month < 10 ? "0" + this.month : this.month)
      );
      this.data = data;
      // console.log("获取的数据", this.data, this.data[0].maketime.substr(5, 2));
      this.data.unshift({ top: this.data[0].maketime.substr(5, 2) });
      console.log("父组件传进来的数据", this.newYear, this.newMonth);
    },
    addData() {
      //scrollTop就是触发滚轮事件时滚轮的高度
      // let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      let scrollTop = this.$refs.readbox.scrollTop;
      let scrollHeight = this.$refs.readbox.scrollHeight; //滚动条的高度
      let clientHeight = this.$refs.readbox.clientHeight; //浏览器的可视高度
      console.log("scrollTop", scrollTop);
      console.log(
        "滚动条高度",
        scrollHeight,
        "浏览器可视高度",
        scrollTop + clientHeight + 100
      );
      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.getLoading();
        }, 1000);
        console.log("触底");
      }
    },
    async getLoading() {
      if (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) < 2) {
        this.oldYear =
          parseInt(this.data[this.data.length - 1].maketime.substr(0, 4)) - 1;
        this.oldMonth = "12";
      } else {
        this.oldYear = parseInt(
          this.data[this.data.length - 1].maketime.substr(0, 4)
        );
        this.oldMonth =
          parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1 <
          10
            ? "0" +
              (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
                1)
            : parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
              1;
      }
      const { data } = await getRead(1, this.oldYear + "-" + this.oldMonth);
      console.log("年月份", this.oldYear, this.oldMonth);
      console.log(
        "裁剪的数据",
        parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1
      );
      // 每组数据前添加个月份top，用来展示
      this.data.push({ top: data[data.length - 1].maketime.substr(5, 2) });
      this.data.push(...data);
      console.log("新数据", this.data);
    },
    goviewArticles(id, category, title, titleTR) {
      // // 跳转到文章详情页
      // if(category==='1'){
      console.log("跳转的id", id);
      this.$router.push({
        path: "/articlesPage",
        query: {
          id: id,
          category: category,
          title: JSON.stringify(title),
          titleTR: encodeURIComponent(titleTR),
        },
      });
      // }else if(category==='3'){
      //   this.$router.push({
      //     path:'/answer',
      //     query:{
      //      id:id,
      //      category:category,
      //      title: JSON.stringify(title),
      //      titleTR:encodeURIComponent(titleTR)
      //    }
      //   })
      // }
      console.log("触发了");
    },
  },
};
</script>
 
<style lang="less" scoped>
.readBox {
  max-height: 667px;
  padding: 0 15px;
  overflow: scroll;
  /*margin-top: 30px;*/
  background: #fcfcfc;
  .readItem {
    display: flex;
    margin: 10px 0;
    img {
      width: 50px;
      height: 50px;
      border-radius: 10px;
    }
    .readInfo {
      margin-left: 10px;
      & > div:nth-child(1) {
        font-size: 14px;
        color: #333333;
        margin-bottom: 5px;
      }
      & > div:nth-child(2) {
        font-size: 12px;
        color: #c3c3c3;
      }
    }
  }
}
</style>